######## Classifier Data Analysis ########
######## Final Year Project 2011#####
######## Making Web Search Spatial###


######## Liam Sullivan       ########
######## University of Leeds ########

import csv


#Read in Queries

QueryListFile = open("QueryListFile.txt")

QueryList = []

count = 0

for line in QueryListFile.readlines():
    
    QueryList.append(str(line))
    count += 1
    print "Read query number: " + str(count) + " which is: " + line
    
print "Read in " + str(count) + " queries to pull data for"




AllData = []


def getQueryData(query):
    """Return a list of any row in csv that has the request query"""
    
    OutList = []
    
    CSVReader = csv.reader(open('data.csv', 'rb'), delimiter=',', quotechar='"')
    
    for row in CSVReader:
        
        #if this row is of interest
        if row[0] == query:
            ##Add to the output list
            OutList.append(row)
    return OutList

def getClass(index):
    """Maps index column number to class"""

    if index == 4:
        return "Arts"
    if index == 5:
        return "Business"
    if index == 6:
        return "Games"
    if index == 7:
        return "Home"    
    if index == 8:
        return "Health"
    if index == 9:
        return "Society"
    if index == 10:
        return "Sports"
    if index == 11:
        return "Recreation"
    if index == 12:
        return "Computers"
    if index == 13:
        return "Science"
  

outWriter = csv.writer(open('results.csv', 'wb'), delimiter=',', quotechar='"')
    
#Write out headings
outWriter.writerow(["Query", "Most number of Topics", "Least number of topics", "Average number of topics", "Topics Hit", "List of Topics hit in Query"])            
          
#Classifications below this are ignored          
Threshold = 10  
            
for query in QueryList:
    QueryData = getQueryData(query)
    print "Query data for: "
    print query
    print " is below: "
    print QueryData
    
    #Check each classifier field

    ClassList = []
    
    MostClasses = 0
    LeastClasses = 10
    ResultCount = 0
    TotalNonZeroClassifications = 0
    
    TopicsHitInQuery = []
    ResultPairsInQuery = []
    
    for result in QueryData:
        ResultCount = ResultCount + 1
        #For each result count how many classifier data there are
        
        ClassCount = 0
        
        #Classification Dictionary for each result
        ClassifierData = {}
        
        for i in range(4,13):
            #Loop through 10 runs to catch all classifier data
            
            if int(result[i]) > Threshold:
                
                #Add this to the dicrionary
                ClassifierData[getClass(i)] = int(result[i])
                
                #If this topic is being hit for the first time in this query then add it to the list
                if not getClass(i) in TopicsHitInQuery:
                    TopicsHitInQuery.append(getClass(i))
                    
                    ##What is the most popular pair?
                    
                    #How maby times times this pair make up the classifier total?
                    
                    #What is this as a proportion of the total classifications i.e. / 100 resutls for this query>?
                    
                    
                    ###Questions to ask
                    # What is the most popular co occurence - i.e. what is the most popular pair of classes hit for this query?
                    #What is the second most popular pair...?              
                
                    
                ##Add this result pair to the list                
                
                TotalNonZeroClassifications = TotalNonZeroClassifications + 1
                ClassCount = ClassCount + 1
            
                ClassList.append([getClass(i), int(result[i])])
                
                if ClassCount == 9:
                    print "This one used 9"
                    
        
        
        print "Classification data for that result was made up of: " + str(ClassCount) + " different classes!"
        
        if ClassCount > MostClasses:
            MostClasses = ClassCount
            
        if ClassCount < LeastClasses:
            LeastClasses = ClassCount        
        #write a line out for this query
        
    print "For that search query the data has been written to file"
    
    #Average = total classifications over number of results
    
    average = int(TotalNonZeroClassifications) /  int(ResultCount)   
    # Query", "Most number of Topics", "Least number of topics", "Average nber of topics"])            
      
    outWriter.writerow([query,MostClasses,LeastClasses, average, len(TopicsHitInQuery), str(TopicsHitInQuery)])
    
            
    
    

    
    

            
    

